package com.mstore.action;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.SessionAware;

import com.mstore.pojo.Account;
import com.mstore.pojo.BWeixinurl;
import com.mstore.service.BWeixinurlService;

public class WeixinUrlAction extends BaseAction implements SessionAware {

	private static final long serialVersionUID = 2088067511207752239L;

	private BWeixinurlService bWeixinurlService;

	private Map<String, Object> session;

	private String result;

	private List<BWeixinurl> weixinurls;

	private BWeixinurl weixinurl;

	private static Logger logger = Logger.getLogger(MemberAction.class
			.getName());

	private Long eaId;

	/**
	 * 查询企业跳转链接
	 */
	public String watchAllWeixinUrl() {
		Object object = session.get("account");
		if (object == null) {
			result = "系统异常，请重新登陆！";
			return "error";
		}
		Account account = (Account) object;

		eaId = account.getId();
		try {
			weixinurls = bWeixinurlService.getWeixinurlByEaId(eaId);
			for (BWeixinurl wxUrl : weixinurls) {
				String url = wxUrl.getUrl();
				if (url.length() > 60) {
					url = url.substring(0, 60) + "...";
				}
				wxUrl.setUrl(url);
			}
		}
		catch (Exception e) {
			logger.error("微信链接查询报错", e);
		}

		return "watchAllWeixinUrl";
	}
	
	/**
	 * 添加微信跳转链接
	 */
	public String addWeixinUrl() {
		if (weixinurl == null) {
			result = "系统异常，请重新登陆！";
			return "error";
		}
		
		weixinurl.setCreateTime(getCreateTime());
		bWeixinurlService.addWeixinUrl(weixinurl);
		
		return "Success";
	}
	
	/**
	 * 删除跳转链接
	 */
	public String delWeixinUrl() {
		if (weixinurl == null) {
			result = "系统异常，请重新登陆！";
			return "error";
		}
		
		bWeixinurlService.delWeixinUrl(weixinurl);
		return "Success";
	}

	/**
	 * 跳转到修改页面
	 */
	public String initModifyWeixinUrl() {
		if (weixinurl == null) {
			result = "系统异常，请重新登陆！";
			return "error";
		}
		
		weixinurl = bWeixinurlService.getWeixinurlById(weixinurl);
		if (weixinurl == null) {
			result = "系统异常，请重新登陆！";
			return "error";
		}
		
		return "initModifyWeixinUrl";
	}
	
	/**
	 * 跳转到添加页面
	 */
	public String toAddWeixinUrl() {
		if (weixinurl == null) {
			result = "系统异常，请重新登陆！";
			return "error";
		}
		return "toAddWeixinUrl";
	}
	
	/**
	 * 修改链接
	 */
	public String modifyWeixinUrl() {
		if (weixinurl == null) {
			result = "系统异常，请重新登陆！";
			return "error";
		}
		
		bWeixinurlService.modifyWeixinUrl(weixinurl);
		return "Success";
	}

	public BWeixinurlService getbWeixinurlService() {
		return bWeixinurlService;
	}

	public void setbWeixinurlService(BWeixinurlService bWeixinurlService) {
		this.bWeixinurlService = bWeixinurlService;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public List<BWeixinurl> getWeixinurls() {
		return weixinurls;
	}

	public void setWeixinurls(List<BWeixinurl> weixinurls) {
		this.weixinurls = weixinurls;
	}

	public BWeixinurl getWeixinurl() {
		return weixinurl;
	}

	public void setWeixinurl(BWeixinurl weixinurl) {
		this.weixinurl = weixinurl;
	}

	public Long getEaId() {
		return eaId;
	}

	public void setEaId(Long eaId) {
		this.eaId = eaId;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
}
